<template>
  <div class="data-table-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.params" inline>
        <el-form-item class="!mr-4" prop="templateId" style="width: 11%">
          <el-select v-model="state.tableData.param.params.templateId" clearable
                     placeholder="请选择优惠券"
                     size="default">
            <el-option
                v-for="item in state.couponList"
                :key="item.id"
                :label="item.couponName"
                :value="item.id"
            >
              <span style="float: left">
                    {{ item.couponName }}
                  </span>
              <span style="float: right; color: #8492a6; font-size: 13px; margin-left: 5px;">
                    {{ item.purpose }}
                  </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="couponType" style="width: 11%;">
          <el-select v-model="state.tableData.param.params.couponType" clearable
                     placeholder="请选择券类型"
                     size="default">
            <el-option
                v-for="item in state.typeList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" highlight-current-row
                style="width: 100%"
                @sort-change="handleSortChange">
        <el-table-column label="ID" prop="id" show-overflow-tooltip/>
        <el-table-column label="券名称" prop="templateName" show-overflow-tooltip/>
        <el-table-column label="券类型" prop="couponType">
          <template #default="scope">
            <dict-tag :options="coupon_type" :value="scope.row.couponType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="会员" prop="memberName"/>
        <el-table-column label="券状态" prop="state">
          <template #default="scope">
            <dict-tag :options="coupon_state" :value="scope.row.state"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" prop="startTime" show-overflow-tooltip sortable/>
        <el-table-column label="结束时间" prop="endTime" show-overflow-tooltip sortable/>
        <el-table-column label="发送时间" prop="sendTime" show-overflow-tooltip sortable/>
        <el-table-column label="发送途径" prop="sendRoute" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="send_route" :value="scope.row.sendRoute"></dict-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.param.pageNum"
                     v-model:page-size="state.tableData.param.pageSize" :page-sizes="[10, 20, 30]" :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <shiftDialog ref="shiftDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, getCurrentInstance, onMounted, reactive, ref} from 'vue';
import {couponTemplateApi} from "/@/api/market/coupon/template";
import {dictDataApi} from "/@/api/sys/dictData";
import DictTag from "/@/components/DictTag/index.vue";
import {couponUsedApi} from "/@/api/market/coupon/used";

// 引入组件
const shiftDialog = defineAsyncComponent(() => import('/@/views/market/coupon/record/dialog.vue'));

const {proxy} = getCurrentInstance();
const {coupon_type, coupon_state, send_route} = proxy.useDict("coupon_type", "coupon_state", "send_route");

// 定义变量内容
const shiftDialogRef = ref();
const queryFormRef = ref();
const state = reactive<any>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      params: {
        templateId: '',
        couponType: ''
      },
    },
  },
  typeList: [],
  couponList: [],
});

const handleSortChange = (data: any) => {
  if (data.order) {
    if (data.order.indexOf('asc') > -1) {
      state.tableData.param.sortOrder = 'asc'
    } else {
      state.tableData.param.sortOrder = 'desc'
    }
    state.tableData.param.sortName = data.prop
  }
  getTableData();
};

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  couponUsedApi()
      .getTableData(state.tableData.param)
      .then((res) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();

  // 券分类
  dictDataApi()
      .getDictByType("coupon_type")
      .then(res => {
        state.typeList = res.data;
      })

  // 优惠券数据
  couponTemplateApi()
      .findAll()
      .then(res => {
        state.couponList = res.data;
      })
});
// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
